//
//  NewsArticleViewController.m
//  JiZhuiAn
//
//  Created by roger lin on 16/7/6.
//  Copyright © 2016年 Easy. All rights reserved.
//

#import "NewsArticleViewController.h"

@interface NewsArticleViewController ()

@property (nonatomic, strong) NewsArticleViewModel * viewModel;

@end

@implementation NewsArticleViewController
@dynamic viewModel;

- (void)viewDidLoad
{
    /*
    UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 480, screenWidth, screenHeight)];
    textView.text = @"12345";
    // 设置文本字体
    textView.font = [UIFont fontWithName:@"Arial" size:16.5f];
    // 设置文本颜色
    textView.textColor = [UIColor colorWithRed:51/255.0f green:51/255.0f blue:51/255.0f alpha:1.0f];
    // 设置文本框背景颜色
    textView.backgroundColor = [UIColor colorWithRed:254/255.0f green:254/255.0f blue:254/255.0f alpha:1.0f];
    // 设置文本对齐方式
    textView.textAlignment = NSTextAlignmentLeft;
    
    textView.delegate = self;
    
    [self.view addSubview:textView];
    
    UIImage *picImage = [UIImage imageNamed:@""];
    
    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 100, 300, 300)];
    imageView.image = picImage;
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(loadTheBigPicture:)];
    imageView.userInteractionEnabled = YES;
    [imageView addGestureRecognizer:singleTap];
    [self.view addSubview:imageView];
    
    */
    [self loadThePicture];
}

- (void)loadThePicture
{
    [MBProgressHUD showHUDinKeyWindow];
    [[[APIService shareService] loadTheContextPictureWithWebsiteTitle:self.viewModel.websiteTitle] subscribeNext:^(NSArray * array) {
        
        BmobFile *file = [[array firstObject] objectForKey:@"imageFile"];
        NSArray * pixels = [[array firstObject] objectForKey:@"pixels"];
        if (file.url) {
            NSURL *url = [NSURL URLWithString:file.url];
            
            UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, navHeight, screenWidth, screenHeight)];
            scrollView.backgroundColor = [UIColor blackColor];
            scrollView.delegate = self;
            NSInteger picWidth = [pixels.firstObject integerValue];
            NSInteger picHeight = [pixels.lastObject integerValue];
            scrollView.contentSize = CGSizeMake(screenWidth, picHeight*screenWidth/picWidth);
            
            UIImageView *bigImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, picHeight*screenWidth/picWidth)];
            [bigImageView sd_setImageWithURL:url];
            
            [scrollView addSubview:bigImageView];
            [self.view addSubview:scrollView];
            
        }
        [MBProgressHUD hideHUDinKeyWindow];
    } error:^(NSError *error) {
        [MBProgressHUD showQuickTipWithTitle:nil withText:[error localizedDescription]];
        [MBProgressHUD hideHUDinKeyWindow];
    }];
     
}

- (void)loadTheBigPicture:(UIImage *)image
{
//    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
    
//    UITapGestureRecognizer *singleCancelTap = [UITapGestureRecognizer alloc]initWithTarget:self action:<#(nullable SEL)#>
//    scrollView addGestureRecognizer:<#(nonnull UIGestureRecognizer *)#>
    scrollView.backgroundColor = [UIColor blackColor];
    scrollView.delegate = self;
    int picWidth = 440;// imageView.frame.size.width;
    int picHeight = 3221;//1466;//imageView.frame.size.height;
    scrollView.contentSize = CGSizeMake(screenWidth, picHeight*screenWidth/picWidth);
    
    UIImageView *bigImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, picHeight*screenWidth/picWidth)];
    bigImageView.image = originalImage2;
    
    [scrollView addSubview:bigImageView];
    
    [self.view addSubview:scrollView];
}



@end
